package com.redoor.app.stake.workflow.service.impl;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.RegistrationApplyForRedis;
import com.redoor.app.stake.basicdata.service.BaseRegistrationService;
import com.redoor.app.stake.basicdata.service.BaseSystemOperationLogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.model.BaseWorkorder;
import com.redoor.app.stake.workflow.model.controllerModel.WorkOrderCriteria;
import com.redoor.app.stake.workflow.service.BaseWorkorderOperationLogService;
import com.redoor.app.stake.workflow.service.BaseWorkorderService;
import com.redoor.app.stake.workflow.service.RegistrationApplyWorkOrderService;
import com.redoornetwork.framework.core.exception.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegistrationApplyWorkOrderServiceImpl implements RegistrationApplyWorkOrderService {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BaseRegistrationService baseRegistrationService;

    @Autowired
    private BaseWorkorderService baseWorkorderService;

    @Autowired
    private BaseWorkorderOperationLogService operationLog;

    @Autowired
    private BaseSystemOperationLogService baseSystemOperationLogService;

    @Autowired
    private BaseUserService userService;

    @Override
    public int create(RegistrationApplyForRedis RegistrationApplyForRedis) {
        if (null == RegistrationApplyForRedis
                || StringUtils.isBlank(RegistrationApplyForRedis.getUuid())
                || StringUtils.isBlank(RegistrationApplyForRedis.getOuCode())){
            throw new ServiceException(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        BaseWorkorder workorder = new BaseWorkorder();
        /*生成uuid由公共的insert方法转移到这个地方,因为保存工单操作日志的时候需要这个uuid*/
        workorder.setUuid(UUIDTool.getUUID());
        /*保存工单类型    targettype*/
        workorder.setTargettype(WorkFlowStatus.WORKORDER_TARGET_TYPE_REGISTRATIONAPPLY.getCode());
        /*保存目标  targetuuid*/
        workorder.setTargetuuid(RegistrationApplyForRedis.getUuid());
        /*保存工单类型名称  content*/
        workorder.setContent(WorkFlowStatus.WORKORDER_TARGET_TYPE_REGISTRATIONAPPLY.getMessage());
        /*保存目标组织机构代码 orgcode*/
        workorder.setCreateorgcode(RegistrationApplyForRedis.getOuCode());
        /*保存申请人uuid*/
        workorder.setCreatoruuid(RegistrationApplyForRedis.getCreatorUuid());

        /*添加操作日志*/
        if (!operationLog.reCreateLog(workorder)){
            logger.error("RegistrationApplyWorkOrderServiceImpl_create_addReCreateError");
        }
        /*添加页面操作日志*/
        baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.ADD,workorder.getUuid(),"",true);

        return baseWorkorderService.insert(workorder);
    }

    @Override
    public int createAll(List<RegistrationApplyForRedis> registrationApplyForRedisList) {
        if(null != registrationApplyForRedisList && 0 < registrationApplyForRedisList.size()){
            for (RegistrationApplyForRedis registrationApplyForRedis : registrationApplyForRedisList){
                // TODO: 2018/4/3 0003 zhangmumu 注册码信息从redis里面批量取出处理,是不是要增加日志
                create(registrationApplyForRedis);
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int accept(WorkOrderCriteria workOrderCriteria) {
        BaseWorkorder baseWorkorder = baseWorkorderService.findById(workOrderCriteria.getUuid());
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_FINISH.getCode()));
        /*写入审核人uuid*/
        baseWorkorder.setReviewruuid(userService.getShiroUserUuid());
        /*写入审核意见*/
        baseWorkorder.setRemark(workOrderCriteria.getReviewSuggestion());
        if(0 < baseWorkorderService.update(baseWorkorder)){
            /*添加操作日志*/
            if(!operationLog.regAcceptLog(baseWorkorder)){
                logger.error("RegistrationApplyWorkOrderServiceImpl_accept_addRegAcceptLogError");
            }
            /*添加页面操作日志*/
            baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);
            return baseRegistrationService.accept(workOrderCriteria.getAcceptedAmount(),baseWorkorder.getTargetuuid(),workOrderCriteria.getReviewSuggestion());
        }else {
            return 0;
        }
    }

    @Override
    public int reject(WorkOrderCriteria workOrderCriteria) {
        BaseWorkorder baseWorkorder = baseWorkorderService.findById(workOrderCriteria.getUuid());
        baseWorkorder.setWorkstatus(Integer.parseInt(WorkFlowStatus.WORKORDER_STATUS_REJECT.getCode()));
        /*写入审核人uuid*/
        baseWorkorder.setReviewruuid(userService.getShiroUserUuid());
        /*写入审核意见*/
        baseWorkorder.setRemark(workOrderCriteria.getReviewSuggestion());
        if (0 < baseWorkorderService.update(baseWorkorder)){
            /*添加操作日志*/
            if (!operationLog.regRejectLog(baseWorkorder)){
                logger.error("RegistrationApplyWorkOrderServiceImpl_reject_addRegRejectLogError");
            }
            /*添加页面操作日志*/
            baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.WORK_ORDER, BussinessTypeEnum.TAG_TYPE.UPDATE,baseWorkorder.getUuid(),"",true);
            return baseRegistrationService.reject(baseWorkorder.getTargetuuid(),workOrderCriteria.getReviewSuggestion());
        }else{
            return 0;
        }
    }
}
